import {
    listCategory
} from "@/api/campus/category";
import {
    handleTree
} from "@/utils/ruoyi";

export async function loadCategoryOptions(loadAllParams) {
    try {
        const response = await listCategory(loadAllParams);
        let categories = handleTree(response.data, "categoryId");
        categories = categories.map(normalizer);
        return categories;
    } catch (error) {
        console.error('获取并规范化分类选项失败:', error);
        throw error;
    }
}
export const normalizer = (node) => {
    if (node.children && node.children.length) {
        node.children = node.children.map(normalizer); // 递归处理子节点
    } else {
        delete node.children; // 如果子节点为空，则删除 children 属性
    }
    return {
        value: node.categoryId, // 使用 categoryId 作为节点的值
        label: node.categoryName, // 使用 categoryName 作为标签
        children: node.children // 子节点数组的键名
    };
};